/**
 * Created by root on 2017/8/14.
 * 新建笔记
 */
 
 function logout(){
    window.location.href="/i/noteuser/noteUser/noteUser";
}
window.onload=function(){
	var username=getCookie("userName");
	var userId=getCookie("userId");
    var mytitle=document.getElementById("mytitle");
    var mytext=document.getElementById("mytext");
    var save_button=document.getElementById("save_button");
    var return_button=document.getElementById("return_button");
	
	var username=getCookie("userName");
	document.getElementById("user_name").innerHTML="您好，"+username;
	
	
    save_button.onclick=function(){
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("POST","/i/note/note/add_note",true);           //保存新建笔记
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        var message_send="userId="+userId+"&title="+mytitle.value+"&text="+mytext.value;
		alert(message_send);
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4)
            {
                if(xmlhttp.status==200)
                {
                    var data=JSON.parse(xmlhttp.responseText);
                    if(data.result==1)
                    {
                        alert("保存成功！");
                        window.location.href="/i/noteuser/noteUser/toMain";
                    }
                }
            }
            else{
                alert("保存失败!");
            }
        }
        xmlhttp.send(message_send);
    }

    return_button.onclick=function(){
        window.location.href="/i/noteuser/noteUser/toMain";
    }
	
	function setCookie(cname,cvalue,exdays){
		var d = new Date();
		d.setTime(d.getTime()+(exdays*24*60*60*1000));
		var expires = "expires="+d.toGMTString();
		document.cookie = cname + "=" + cvalue + "; " + expires;
	} 


	function getCookie(cname){
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i<ca.length; i++) {
			var c = ca[i].trim();
			if (c.indexOf(name)==0) return c.substring(name.length,c.length);
		}
		return "";
	} 
 	
}